﻿// ***********************************************************************
// Assembly         : CommonLayer
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
//
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-06-2013
// ***********************************************************************
// <copyright file="ErrorController.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using System.Web.Security;
using InnovaApp.Layers.CommonLayer.Models.ViewModels.LayoutViewModels;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using INNOVACALL.Framework.Common.InnovacallExceptions;
using INNOVACALL.Framework.Common.MVCHelpers;
using INNOVACALL.Framework.Common.MVCHelpers.Localization;

namespace InnovaApp.Layers.CommonLayer.Controllers.ViewControllers.LayoutViewControllers
{
    /// <summary>
    /// Class ErrorController
    /// </summary>
    public class ErrorController : _LayoutViewControllers_Base<Error_ViewModel>
    {
        /// <summary>
        /// Initialise des données qui peuvent ne pas être disponibles lorsque le constructeur est appelé.
        /// </summary>
        /// <param name="requestContext">Contexte HTTP et données d'itinéraire.</param>
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.eMethodBase = "InnovaApp.Portals.InnovaAppFrontOfficePortal.Controllers.ErrorController";
            base.specificCssCollection = new string[] {"Views/Error.css", "ClientComponents/KendoUI/kendo." + Environment.KendoUITheme + ".min.css" };
            base.specificJSCollection = null;
            base.dynamicJSCollection = new string[] { "/ScriptsG/ErrorDynamic.js" };
            base.specificJqueryJSCollection = new string[] { "Views/Error.js" };
            base.externalCss = null;

            base.Initialize(requestContext);
        }

        /// <summary>
        /// Affichage du contenu de la page d'accueil
        /// Warning, it must works for all requests GET/POST...
        /// </summary>
        /// <param name="friendlyUrl">The friendly URL.</param>
        /// <param name="controller">The controller.</param>
        /// <param name="action">The action.</param>
        /// <param name="errorCode">The error code.</param>
        /// <param name="language">The language.</param>
        /// <param name="exception">The exception.</param>
        /// <returns>ActionResult.</returns>
        [CacheManager(enCacheMode.NoCache)]
        public ActionResult Index
           (
                string friendlyUrl,
                string controller,
                string action,
                int errorCode,
                string language,
                Exception exception
            )
        {
            var UserLanguages = Request.UserLanguages;

            string pageTitle = string.Format(Resources.View_Error_PageTitle, errorCode);
            string message = "";

            using (DataRepositories _dataContext = new DataRepositories())
            {
                base.InitView(
                    pageTitle,
                    string.Empty,
                    specificCssCollection,
                    specificJSCollection,
                    dynamicJSCollection,
                    specificJqueryJSCollection,
                    jsVariables,
                    externalJs,
                    externalCss,
                    Meta,
                    _dataContext,
                    true,
                    language,
                    false,
                    true,
                    Environment.JQueryUITheme,
                    Environment.KendoUITheme,
                    Membership.GetUser()
                    );
            }


            ContentData.ErrorTitle = pageTitle;
            StringBuilder oSB = new StringBuilder();
            if (exception == null)
            {
                CommonLocalization.LookupResource(typeof(Resources), string.Format(@"View_HttpError_{0}", errorCode));
                ContentData.ErrorMessage = CommonLocalization.LookupResource(typeof(Resources), string.Format(@"View_HttpError_{0}", errorCode));
                return View(ContentData);
            }
            if (typeof(I_InnovacallExceptions_Base).IsAssignableFrom(exception.GetType()))
            {
                I_InnovacallExceptions_Base InnoException = (I_InnovacallExceptions_Base)exception;
                oSB.AppendLine("<p><b>Exception type :</b> " + InnoException.ExceptionType.ToString() + "</p>");
                oSB.AppendLine("<p><b>Functional message :</b> " + InnoException.FunctionalMessage + "</p>");
                oSB.AppendLine("<p><b>Method :</b> " + InnoException.Method + "</p>");
                foreach (var prop in InnoException.MethodParameters.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    oSB.AppendLine("<p>" + string.Format("<b>Name</b> : {0}, <b>Value :</b> {1}", prop.Name, prop.GetValue(InnoException.MethodParameters, null)) + "</p>");
                }
            }
            else
            {
                oSB.AppendLine("<p>" + CommonLocalization.LookupResource(typeof(Resources), string.Format("View_HttpError_{0}", errorCode)) + "</p>"); ;
                oSB.AppendLine("<p>" + exception.Message + "</p>"); ;
            }
            message = oSB.ToString();
            ContentData.ErrorMessage = message;
            if (exception != null)
            {
                ContentData.ErrorData = exception.StackTrace;
            }
            return View(ContentData);
        }
    }
}
